package lib;
import java.io.*;
import java.util.*;
import java.net.*;

import com.thoughtworks.xstream.*;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
/**
 * Used to perform a search on the library's website.
 * Benchmark:
 * LibSearch: 10431ms for default settings
 * TestRun: 11911ms for default settings
 * @author smy
 * @version alpha1
 * 
 */
public class LibSearch {
    private static final String static_1="http://202.119.47.8:8080/opac/search_adv_result.php?sort=";
    private String para_sort="score"; //Default is "score"
    private String para_desc="false"; //Default is "false"
    private String para_sType0="any"; //Default is "any"
    private String para_q0="oracle"; //Search term
    private String para_topage="1"; //Default is the first page
    private String para_pagesize="5"; //Default is not 5
    private ArrayList <Book> books=new ArrayList <Book>();
    private XStream xs=new XStream();
/**
 * Program entrypoint.
 * @param args 6 parameters. Six exactly!
 * args[6]:{para_sort,para_desc,para_sType0,para_q0,para_topage,para_pagesize}
 * 1. para_sort: The method chosen for sorting the results.
 *    "score"<---similarity;
 *    "title"<---title;
 *    "callNo"<---requestID;
 *    "publisher"<---publisher;
 *    "pubYear"<---year published.
 * 2. para_desc: Whether descending or not?
 *    "true"<-----descending
 *    "false"<----ascending
 * 3. para_sType0: Search term is based on what category?
 *    "any"<----any category
 *    "02"<---title
 *    "03"<---author
 *    "04"<---keyword
 *    "08"<---requestid
 *    "09"<---publisher
 *    "10"<---nameofcollection(?)
 * 4. para_q0: The exact search term.
 * 5. para_topage: Which page?
 * 6. para_pagesize: How many entries per page?
 * @category Entrypoint
 *    
 */
    public static void main(String[] args){
    	if(args.length!=6){
    		System.err.println("Invaild input");
    		System.exit(1);
    	}
    	LibSearch ls=new LibSearch();
    	ls.para_sort=args[0];
    	ls.para_desc=args[1];
    	ls.para_sType0=args[2];
    	ls.para_q0=args[3];
    	ls.para_topage=args[4];
    	ls.para_pagesize=args[5];
    	try {
			ls.go();
		} catch(Exception ex){
			System.err.println("An error occured.");
			System.err.println(ex.getMessage());
			System.exit(2);
		}
    }
    private void go() throws FailingHttpStatusCodeException, IOException{
    	long a=System.currentTimeMillis();
    	WebClient client=new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
    	client.setCssEnabled(false);
    	client.setJavaScriptEnabled(false);
    	URL searchurl=new URL(static_1+para_sort+"&desc="+para_desc+"&sType0="+para_sType0+"&q0="+para_q0+"&topage="+para_topage+"&pageSize="+para_pagesize);
    	HtmlPage result_page=client.getPage(searchurl);
    	HtmlTable result_table=(HtmlTable)(result_page.getElementsByTagName("table").get(0));
    	int num_found=1;
    	String title="",publisher="",author="",reqid="",type="",marc_no="";
    	while(num_found<result_table.getRowCount()){ //Extract information from the table containing details
    		title=result_table.getRow(num_found).getCell(1).asText();
    		author=result_table.getRow(num_found).getCell(2).asText();
    		publisher=result_table.getRow(num_found).getCell(3).asText();
    		reqid=result_table.getRow(num_found).getCell(4).asText();
    		type=result_table.getRow(num_found).getCell(5).asText();
    		marc_no=result_table.getRow(num_found).getCell(1).asXml().split("marc_no=")[1].substring(0,10);
    		Book book_found=new Book(num_found,title,author,publisher,reqid,type,marc_no);
    		this.books.add(book_found);
    		num_found++;
    	}
    	FileOutputStream fos=new FileOutputStream("d:/BSWD/test.xml");
        xs.toXML(books,fos);
    	System.out.println("Done in "+(System.currentTimeMillis()-a)+" ms.");
    }
}
